#ifndef _MUNIX_FILESYSTEM_H
#define	_MUNIX_FILESYSTEM_H
/*******************************************************************************
 ** Name: filesystem.h                                                        **
 ** Description: A set of file system definitions for munixOS.                **
 **                                                                           **
 ** Open Source Initiative (OSI) Approved License                             **
 **                                                                           **
 ** The contents of this file are subject to the terms of the                 **
 ** Common Development and Distribution License, Version 1.0 only             **
 ** (the "License").  You may not use this file except in compliance          **
 ** with the License.                                                         **
 **                                                                           **
 ** You can find a copy of the license in the license.txt within              **
 ** this distribution or at http://www.munixos.net/licensing.                 **
 ** Software distributed under the License is distributed on an "AS IS"       **
 ** basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.           **
 ** See the License for the specific language governing permissions           **
 ** and limitations under the License.                                        **
 **                                                                           **
 ** When distributing Covered Code, include this CDDL header in each          **
 ** file and include the License file at $HOME/license.txt.                   **
 ** If applicable, add the following below this header, with the indicated    **
 ** fields enclosed by brackets "[]" replaced with your own identifying       **
 ** information: Portions Copyright [yyyy] [name of copyright owner]          **
 **                                                                           **
 **                                                                           **
 ** Copyright (c) 2009-2010  Barry Gian James.                                **
 ** All rights reserved.                                                      **
 **                                                                           **
 ** Ref: $HeadURL$
 ******************************************************************************/
// Portions (C) 2011-12 Open Design Strategies, LLC.

// Last Modified $DateTime: 2010/02/12 01:42:30 $ by $Author$

#define MUNIX_FILESHSTEM_H_ID "$Id$"

#include <common.h>
#include <int24>

#define AVRFS __attribute__ ((section(".avrfs")))

// Flash file systems require some special care that normal disk drive storage
// never worried about, like low endurance (ie Flash can only be written to
// so many times), so this must be taken into consideration in the design.

//#define DEFAULT_BLOCK_SIZE  1024    // bytes
#define DEFAULT_NEIGHBORHOOD 65535  // 64K Bytes
#define DEFAULT_VOL_SIZE       0     // TODO
#define DEFAULT_SECTOR_SIZE_SM 4092
#define DEFAULT_SECTOR_SIZE_MED 32768
#define DEFAULT_SECTOR_SIZE_LG	65535
#define DEFAULT_PAGE_SIZE   256     // bytes
#define MAX_FILE_LEN		32
#define MAX_PATH_DEPTH      255

extern const char __def_path_sep;         // default path separator
extern const char __def_reg_tok;          // default register token



// Supported file systems
enum class fsType { E2FS, AVRfs, TUDOR, FAT };

// File system objects have rudimentary access control
enum class fsACLType : uint8_t { Read=0x01, Write=0x02, Delete=0x04, See=0x08, Exec=0x10 };
enum class fsACLGroups : uint8_t { Owner=0x01, Group=0x02, World=0x04 };
struct fsACL
{
    uint8_t  	owner;  // maximum 256 users per system
    fsACLType   acl;

    bool	CanRead() { return acl & fsACLType::Read; }
    bool	CanWrite() { return acl & fsACLType::Write; }
    bool	CanDelete() { return acl & fsACLType::Delete; }
    bool	CanSee() { return acl & fsACLType::See; }
    bool	CanExec() { return acl & fsACLType::Exec; }
};

enum class fsNodeType : uint8_t { File, Directory, Device, Register, Byte, Word };
struct fsNodeInfo
{								// Bytes
    fsNodeType      type;		// 1
    fsACL           owner;		// 3
    fsACL           group;		// 5
    fsACL           other;		// 7
    size_t          size;		// 9
	time_t			createTime;	// 13
	time_t			modifiedTime;;	// 17
	uint8_t			refs;			// 18
};

//struct fsFreeNode
//{
//	FlashAddr	addr;
//	fsFreeNode * back;
//	fsFreeNode * forward;
//};

//! class fsNode
struct fsNode
{
	char	name[MAX_FILE_LEN];
	uint32	id;
    fsNodeInfo  info;
//    FlashAddr	dataBlocks[12];
//	FlashAddr	indirectBlocks[2];
	uint24_t	nextFree;


	fsNode * dotdot;
    fsNode * next;
};


class FileSystem
{
public:

	FileSystem();
	virtual ~FileSystem();
/*
	fsNode *	OpenDir(const char *);
	fsNode *	ReadDir(fsNode *);
	void		CloseDir(fsNode *);
	void		RmDir(fsNode *);
	void		CreateDir(fsNode *);

	fsNode *	Open(const char *);
	fsNode *	Read(fsNode *);
	void		Rm(fsNode *);
	void		Create(const char *);
*/
private:
	fsNode *	root;


};

#endif	/* _MUNIX_FILESYSTEM_H */

